#!/bin/sh
# Script to install debian 6 "squeeze" release into a jail
# Author: Kris Moore <kris@pcbsd.org>

SELF="debian-6-squeeze"
SELFDIR="${JDIR}/.${SELF}"
ERRORFILE="/tmp/.${SELF}.error"

get_distfiles()
{
    rm -f "${ERRORFILE}"

    if [ ! -d "${SELFDIR}" ] ; then
      mkdir -p "${SELFDIR}"
    fi
}

template_install()
{
    rm -f "${ERRORFILE}"

    local TDIR="${1}"
    if [ -z "${TDIR}" ] ; then 
      echo "TDIR is not defined"
      touch "${ERRORFILE}"      
      exit 1
    fi

    debootstrap squeeze ${TDIR}

    umount -f ${TDIR}/dev/fd >/dev/null 2>/dev/null
    umount -f ${TDIR}/dev >/dev/null 2>/dev/null
    umount -f ${TDIR}/sys >/dev/null 2>/dev/null
    umount -f ${TDIR}/proc >/dev/null 2>/dev/null

    # Fix log file
    sed -i '' 's|/dev/xconsole|/var/log/xconsole|g' ${TDIR}/etc/rsyslog.conf

    # Get rid of umount
    mv ${TDIR}/sbin/umount ${TDIR}/sbin/umount.prev
    ln -s /bin/true ${TDIR}/sbin/umount
}

jail_configure()
{
    rm -f "${ERRORFILE}"

    local JMETADIR="${1}"
    if [ -z "${JMETADIR}" ] ; then 
      echo "JMETADIR is not defined"
      exit 1
    fi

    # Setup some custom start / stop stuff
    echo "/etc/init.d/rc 3" > ${JMETADIR}/jail-start
    echo "/etc/init.d/rc 0" > ${JMETADIR}/jail-stop
}

error()
{
    if [ -f "${ERRORFILE}" ] ; then
        rm -f "${ERRORFILE}"
        return 1
    fi

    return 0
}

main()
{
    case "${1}" in
      get_distfiles) shift; get_distfiles "$@" ;; 
      template_install) shift; template_install "$@" ;;
      jail_configure) shift; jail_configure "$@" ;;
      error) shift; error "$@" ;;
    esac

    if [ "$?" != "0" ] ; then
      touch "${ERRORFILE}"
    fi
}


main "$@"
